﻿using System.ComponentModel;
using System.Runtime.Serialization;
using GameLibrary.Internal.Converters;

namespace G.Model
{
    [DataContract]
    public class GameGlobalDef
    {
        public delegate void GlobalHandler(GameSpaceDef def);
        public event GlobalHandler Changed;
        
        private GameSpaceDef _GameDef;
        
        [DataMember(Name="GameValueDefs")]
        private readonly GameValueDefs _GameValueDefs;
        
        [DataMember(Name="GamePlayerValueDefs")]
        private readonly GameValueDefs _GamePlayerValueDefs;
        
        [DataMember(Name="GameEventDefs")]
        private readonly GameEventDefs _GameEventDefs;

        [DataMember(Name="DefaultIcon")]
        private string _DefaultIcon;

        public GameGlobalDef(GameSpaceDef pGameDef)
        {
            _GameDef = pGameDef;
            _GameValueDefs = new GameValueDefs(_GameDef);
            _GamePlayerValueDefs = new GameValueDefs(_GameDef);
            _GameEventDefs = new GameEventDefs();
        }

        [IgnoreDataMember]
        public GameValueDefs GameValueDefs
        {
            get { return _GameValueDefs; }
        }

        [IgnoreDataMember]
        public GameValueDefs GamePlayerValueDefs
        {
            get { return _GamePlayerValueDefs; }
        }

        [IgnoreDataMember]
        public GameEventDefs GameEventDefs
        {
            get { return _GameEventDefs; }
        }
        
        [IgnoreDataMember, Browsable(false) ]
        public GameSpaceDef GameDef
        {
            get { return _GameDef; }
            set { _GameDef = value; }
        }

        [TypeConverter(typeof(IconStringConverter))]
        public string DefaultIcon
        {
            get { return _DefaultIcon; }
            set { 
                _DefaultIcon = value;
                foreach (GameElementDef def in _GameDef.GameElementDefs) 
                {
                    _GameDef.GameElementDefs.FireElementPropertyChanged(def);
                }
                _GameDef.IsDirty = true;
                FireChanged();
            }
        }
        
        internal void FireChanged()
        {
            if (Changed != null)
            {
                Changed(_GameDef);
            }
        }
    }
}
